﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using SpriteGame.Rendering;
using Microsoft.Xna.Framework;
using SpriteGame.Helpers;
using Microsoft.Xna.Framework.Input;

namespace SpriteGame.UI
{
    public class UIContainerPanel : UIPanel
    {
        public UIContainerPanel(int _left, int _top, int _width, int _height)
            : base(_left, _top, _width, _height)
        {
        }

        public override void RenderContent(SpriteBatch _batch, Rectangle _rect)
        {
            base.RenderContent(_batch, _rect);
        }
    }

    public class UIImageList : UIPanel
    {
        string background;
        string[] images;
        int index;
        public int Index
        {
            get { return index; }
        }
        int nextImage = 0;
        int prevImage = 0;

        public String Image
        {
            get { return images[index]; }
        }

        Rectangle imageRect = new Rectangle(0, 0, 128, 128);
        Rectangle prevRect = new Rectangle(0, 10, 90, 90);
        Rectangle nextRect = new Rectangle(0, 10, 90, 90);

        int yOffset = 35;

        //UIText label;

        public UIImageList(int _left, int _top, int _width, int _height, string _background, string[] _images)
            : base(_left, _top, _width, _height)
        {

            //label = new UIText(192, 3, 128, 32, "Label Here");
            //children.Add(label);

            images = _images;

            background = _background;

            if (images.Length > 0)
            {
                foreach (string s in images)
                {
                    TextureLibrary.LoadTexture(s);
                }
            }

            index = 1;
            PreviousImage();
            UpdateImages();
        }

        public void NextImage()
        {
            index++;
            if (index > (images.Length - 1))
            {
                index = 0;
            }

            nextImage = index + 1;
            prevImage = index - 1;

            UpdateNumbers();
            UpdateImages();
        }

        public void PreviousImage()
        {
            index--;
            if (index < 0)
            {
                index = (images.Length - 1);
            }

            nextImage = index + 1;
            prevImage = index - 1;

            UpdateNumbers();
            UpdateImages();
        }

        void UpdateNumbers()
        {
            if (prevImage < 0)
            {
                prevImage = (images.Length - 1);
            }

            if (nextImage > images.Length - 1)
            {
                nextImage = 0;
            }

            if (nextImage < 0)
            {
                prevImage = (images.Length - 1);
            }

            if (prevImage > images.Length - 1)
            {
                nextImage = 0;
            }

        }

        void UpdateImages()
        {
            Texture2D image = TextureLibrary.GetTexture(images[index]);
            Texture2D prevImageTex = TextureLibrary.GetTexture(images[prevImage]);
            Texture2D nextImageTex = TextureLibrary.GetTexture(images[nextImage]);

            int middle = boundsRect.X + (width / 2);

            float ratio = (float)image.Height / image.Width;

            imageRect.X = middle - 128;
            imageRect.Y = boundsRect.Y + yOffset;
            imageRect.Height = (int)(128 * ratio);

            ratio = (float)nextImageTex.Height / nextImageTex.Width;
            nextRect.X = middle + 148;
            nextRect.Y = boundsRect.Y + yOffset;
            nextRect.Height = (int)(90 * ratio);

            ratio = (float)prevImageTex.Height / prevImageTex.Width;
            prevRect.X = middle - 328;
            prevRect.Y = boundsRect.Y + yOffset;
            prevRect.Height = (int)(90 * ratio);

            //label.Text = images[index];
        }

        public override void RenderContent(SpriteBatch _batch, Rectangle _rect)
        {
            //base.Render(_batch, position);

            _batch.Draw(TextureLibrary.GetTexture(background), _rect, Color.White);

            _batch.GraphicsDevice.ScissorRectangle = _rect;

            Texture2D image = TextureLibrary.GetTexture(images[index]);
            Texture2D prevImageTex = TextureLibrary.GetTexture(images[prevImage]);
            Texture2D nextImageTex = TextureLibrary.GetTexture(images[nextImage]);

            _batch.Draw(image, imageRect, Color.White);
            _batch.Draw(prevImageTex, prevRect, Color.White);
            _batch.Draw(nextImageTex, nextRect, Color.White);
        }

    }

    public class UIImage:UIPanel
    {
        protected string texture;

        public UIImage(int _left, int _top, int _width, int _height, string _texture)
            :base(_left, _top, _width, _height)
        {
            texture = _texture;
            TextureLibrary.LoadTexture(texture);
        }

        public override void RenderContent(SpriteBatch _batch, Rectangle _rect)
        {
            _batch.Draw(TextureLibrary.GetTexture(texture), _rect, Color.White);
            base.RenderContent(_batch, _rect);
        }
    }

    public class UIMainMenuContainer : UIPanel
    {
        public UIMainMenuContainer(int _left, int _top, int _width, int _height)
            : base(_left, _top, _width, _height)
        {

        }

        int selectedIndex = 0;

        public override void Update(Vector2 _parentPosition)
        {
            if (InputHandler.IsGamepadButtonPressed(Buttons.A))
                children[selectedIndex].FireClick();

            if (InputHandler.IsGamepadButtonPressed(Buttons.DPadUp))
                 previousItem();

            if (InputHandler.IsGamepadButtonPressed(Buttons.DPadDown))
                nextItem();
            
            base.Update(_parentPosition);
        }

        public override void AddChild(UIPanel _child)
        {
            base.AddChild(_child);
            if (children.Count == 1)
            {
                ((UIButtonToggle)_child).Value = true;
            }
        }

        void nextItem()
        {
            ((UIButtonToggle)children[selectedIndex]).Value = false;
            selectedIndex++;
            if (selectedIndex > (children.Count - 1))
            {
                selectedIndex = 0;
            }
            ((UIButtonToggle)children[selectedIndex]).Value = true;
        }

        void previousItem()
        {
            ((UIButtonToggle)children[selectedIndex]).Value = false;
            UIButtonToggle target = (UIButtonToggle)children[selectedIndex];
            selectedIndex--;

            if (selectedIndex < 0)
            {
                selectedIndex = children.Count - 1;
            }
            ((UIButtonToggle)children[selectedIndex]).Value = true;
        }

        public override void RenderContent(SpriteBatch _batch, Rectangle _rect)
        {
            base.RenderContent(_batch, _rect);
        }
    }

    public class UIPercentageBar : UIPanel
    {
        protected string texture;

        float percentage = 1.0f;
        public float Percentage
        {
            get { return percentage; }
            set { percentage = value; }
        }

        public UIPercentageBar(int _left, int _top, int _width, int _height, string _texture)
            : base(_left, _top, _width, _height)
        {
            texture = _texture;
            TextureLibrary.LoadTexture(texture);
            TextureLibrary.LoadTexture(texture + "_Bar");
        }

        public override void RenderContent(SpriteBatch _batch, Rectangle _rect)
        {
            _batch.Draw(TextureLibrary.GetTexture(texture), _rect, Color.White);
            Rectangle baseRect = _rect;
            baseRect.Width = (int)(baseRect.Width * percentage);
            _batch.Draw(TextureLibrary.GetTexture(texture + "_Bar"), baseRect, Color.White);
            base.RenderContent(_batch, _rect);
        }
    }

    public class UIButton : UIImage
    {
        string text = "";

        float halfWidth;
        float halfHeight;

        public UIButton(int _left, int _top, int _width, int _height, string _text)
            : base(_left, _top, _width, _height, "Textures/UI/BasicButton")
        {
            text = _text;
            halfWidth = width * 0.5f;
            halfHeight = height * 0.5f;
        }

        public override void RenderContent(SpriteBatch _batch, Rectangle _rect)
        {
            base.RenderContent(_batch, _rect);
            Vector2 position = new Vector2(boundsRect.X + halfWidth, boundsRect.Y + halfHeight);
            Vector2 halfTextHeight = GlobalFont.MeasureString(text);
            halfTextHeight *= 0.5f;
            _batch.DrawString(GlobalFont, text, position - halfTextHeight, Color.White);
        }
    }

    public class UIButtonToggle : UIImage
    {
        string text = "";

        float halfWidth;
        float halfHeight;

        string normalTexture;
        string pressedTexture;

        bool boolValue = false;
        public bool Value
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        public UIButtonToggle(int _left, int _top, int _width, int _height, string _text)
            : base(_left, _top, _width, _height, "Textures/UI/BasicButton")
        {
            text = _text;
            halfWidth = width * 0.5f;
            halfHeight = height * 0.5f;
            normalTexture = texture;
            pressedTexture = texture + "_Pressed";
            TextureLibrary.LoadTexture(pressedTexture);
        }

        public UIButtonToggle(int _left, int _top, int _width, int _height, string _text, string _texture)
            : base(_left, _top, _width, _height, _texture)
        {
            text = _text;
            halfWidth = width * 0.5f;
            halfHeight = height * 0.5f;
            normalTexture = texture;
            pressedTexture = texture + "_Pressed";
            TextureLibrary.LoadTexture(pressedTexture);
        }

        protected override void LeftClick()
        {
            boolValue = !boolValue;
            base.LeftClick();
        }

        public override void RenderContent(SpriteBatch _batch, Rectangle _rect)
        {
            if (!boolValue)
                texture = normalTexture;
            else
                texture = pressedTexture;

            base.RenderContent(_batch, _rect);
            if (text != "")
            {
                Vector2 position = new Vector2(boundsRect.X + halfWidth, boundsRect.Y + halfHeight);
                Vector2 halfTextHeight = GlobalFont.MeasureString(text);
                halfTextHeight *= 0.5f;
                _batch.DrawString(GlobalFont, text, position - halfTextHeight, Color.White);
            }
        }

    }

    public class UIText : UIPanel
    {
        string text = "";

        bool leftJustify = false;

        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        float halfWidth;
        float halfHeight;

        public UIText(int _left, int _top, int _width, int _height, string _text)
            : base(_left, _top, _width, _height)
        {
            text = _text;
            halfWidth = width * 0.5f;
            halfHeight = height * 0.5f;
        }

        public UIText(int _left, int _top, int _width, int _height, string _text, bool _justify)
            : base(_left, _top, _width, _height)
        {
            text = _text;
            halfWidth = width * 0.5f;
            halfHeight = height * 0.5f;

            leftJustify = _justify;
        }

        public override void RenderContent(SpriteBatch _batch, Rectangle _rect)
        {
            if (leftJustify)
            {
                Vector2 position = new Vector2(boundsRect.X, boundsRect.Y);
                _batch.DrawString(GlobalFont, text, position, Color.White, 0.0f, Vector2.Zero, 0.5f, SpriteEffects.None, 0);
            }
            else
            {
                Vector2 position = new Vector2(boundsRect.X + halfWidth, boundsRect.Y + halfHeight);
                Vector2 halfTextHeight = GlobalFont.MeasureString(text);
                halfTextHeight *= 0.5f;
                //_batch.DrawString(GlobalFont, text, position - halfTextHeight, Color.White);
                _batch.DrawString(GlobalFont, text, position, Color.White, 0.0f, halfTextHeight, 0.5f, SpriteEffects.None, 0);
            }

        }

    }
}
